{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bc62ea1a",
   "metadata": {},
   "source": [
    "# 1.语音识别\n",
    "> 1.百度API：百度的AI账号——(key，secret)  \n",
    "> 2.I/O:input:音频文件(pcm/wav/amr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8e580c38",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdasr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1828522f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.855473\n",
      "{\"corpus_no\":\"7239529191088403324\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"广州天气。\"],\"sn\":\"855938184581685584240\"}\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'{\"corpus_no\":\"7239529191088403324\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"广州天气。\"],\"sn\":\"855938184581685584240\"}\\n'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bdasr.asr('1.wav') #音频文件"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47d548ef",
   "metadata": {},
   "source": [
    "# 2.音频录制\n",
    "* 安装下载如下3个模块：pip install模块名称\n",
    "> 1.SpeechRacognition  \n",
    "> 2.Pyaudio  \n",
    "> 3.pydub  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "40d0f0dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"16k.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c5af7d2",
   "metadata": {},
   "source": [
    "# 3.连接自动回复字典(openai-chatgpt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd8830e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa={\n",
    "    \"你好\":\"好个啥呀!\"\n",
    "    \"北京科技馆\":\"中国科学技术馆,位于北京市朝阳区北辰东路5号,东临亚运居住区,西濒奥运水系,南依奥运主体育场,北望森林公园,于1958年筹建,建设分为两期工程和新馆工程,分别于1988年9月22日,2000年4月29日,2009年9月16日开放.\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14c4b42f",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa={\n",
    "    \"你好\":\"好个啥呀!\"\n",
    "    \"北京科技馆\":\"中国科学技术馆,位于北京市朝阳区北辰东路5号,东临亚运居住区,西濒奥运水系,南依奥运主体育场,北望森林公园,于1958年筹建,建设分为两期工程和新馆工程,分别于1988年9月22日,2000年4月29日,2009年9月16日开放.\"\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3387ed20",
   "metadata": {},
   "source": [
    "# 3.1 接入chatgpt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14b8c715",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install urllib3=1.25"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "92edbea7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import opchat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "557f326d",
   "metadata": {},
   "outputs": [],
   "source": [
    "opchat.openai_chat('你好')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3d6a7cd",
   "metadata": {},
   "source": [
    "# 4.语音合成\n",
    "> 1.百度API：百度的AI账户-(key，secre)  \n",
    "> 2.I/O:input:音频文件(pcm/wav/amr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d44c4de1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdtts\n",
    "bdtts.tts(qa_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3395f013",
   "metadata": {},
   "source": [
    "# 5.播放音频"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "708d67dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
